package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.R;
import com.itheima.common.TokenHolder;
import com.itheima.constant.TokenConstant;
import com.itheima.domian.AddressBook;
import com.itheima.domian.PhoneCode;
import com.itheima.domian.ShoppingCart;
import com.itheima.domian.User;
import com.itheima.domian.vo.UserVo;
import com.itheima.mapper.client.AddressBookMapper;
import com.itheima.mapper.client.UserMapper;
import com.itheima.service.client.AddressBookService;
import com.itheima.utils.RandomCodeUtils;
import com.itheima.utils.SendMessageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author 12441
 * @Date 2022/8/18 14:52
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private AddressBookMapper addressBookMapper;


    @PostMapping
    public R<String>addAddressBook(@RequestBody AddressBook addressBook){

        String currentId = TokenHolder.getCurrentId();
        addressBook.setUserId(currentId);

        addressBookService.save(addressBook);

        return R.success("地址添加成功");
    }
    @GetMapping("/list")
    public R<List<AddressBook>>addressBookList(){

        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,TokenHolder.getCurrentId());

        List<AddressBook> list = addressBookMapper.selectList(queryWrapper);

        return R.success(list,"收货地址展示完成");
    }
    @GetMapping("/{id}")
    public R<AddressBook>showAddressBook(@PathVariable String id){

        AddressBook selectById = addressBookMapper.selectById(id);

        return R.success(selectById,"查询成功");
    }
    @PutMapping
    public R<String>saveAddressBook(@RequestBody AddressBook addressBook){

       addressBookService.updateById(addressBook);

        return R.success("重编辑成功");
    }
    @DeleteMapping
    public R<String>deleteAddressBook(@RequestParam String ids){
        log.info("----------{}", ids);

        String currentId = TokenHolder.getCurrentId();
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, currentId);
        queryWrapper.eq(AddressBook::getId, ids);
        addressBookMapper.delete(queryWrapper);


        return R.success("删除成功");
    }
  @PutMapping("/default")
    public R<AddressBook>setDefault(@RequestBody AddressBook addressBook){

      LambdaUpdateWrapper<AddressBook> queryWrapper = new LambdaUpdateWrapper<>();
      queryWrapper.eq(AddressBook::getUserId,TokenHolder.getCurrentId());
      queryWrapper.set(AddressBook::getIsDefault,0);

      addressBookService.update(queryWrapper);

      addressBook.setIsDefault(1);

      addressBookService.updateById(addressBook);


      return R.success(addressBook);
    }
    @GetMapping("/default")
    public R<AddressBook>getDefault(){

      LambdaUpdateWrapper<AddressBook> queryWrapper = new LambdaUpdateWrapper<>();
      queryWrapper.eq(AddressBook::getUserId,TokenHolder.getCurrentId());
      queryWrapper.eq(AddressBook::getIsDefault,1);

        AddressBook selectOne = addressBookMapper.selectOne(queryWrapper);
        log.info(">>>>>>>"+selectOne);


      return R.success(selectOne);
    }


}